package com.hbzhit.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbzhit.common.extension.BaseController;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.R;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.modules.sys.entity.ProEntity;
import com.hbzhit.modules.sys.entity.ProUserEntity;
import com.hbzhit.modules.sys.entity.SysRoleEntity;
import com.hbzhit.modules.sys.entity.SysUserEntity;
import com.hbzhit.modules.sys.service.ProService;
import com.hbzhit.modules.sys.service.ProUserService;
import com.hbzhit.modules.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qq
 * @date 2022-04-22 16:15:46
 */
@Api(tags = {"项目管理"})
@AllArgsConstructor
@RestController
@RequestMapping("sys/pro")
public class ProController extends BaseController<ProService, ProEntity> {

    @Resource
    private ProUserService proUserService;

    @Resource
    private ProService proService;

    @Resource
    private SysUserRoleService userRoleService;

    @Override
    @ApiOperation("查询当前人员所参与项目")
    public R<PageUtils<ProEntity>> page(@RequestParam Map<String, Object> params) {
        SysUserEntity currUser = ShiroUtils.getUser();
        //获取当前用户角色信息
        List<SysRoleEntity> sysRoleEntityList = userRoleService.queryRoleList(currUser.getId());
        List<String> roleNames = sysRoleEntityList.stream().map(SysRoleEntity::getRoleName).collect(Collectors.toList());
        if (!roleNames.contains("交付总监")) {
            List<ProUserEntity> proUserList = proUserService.list(new LambdaQueryWrapper<ProUserEntity>().eq(ProUserEntity::getUserId, currUser.getId()).eq(ProUserEntity::getIsDel, 0));
            params.put("id_IN", StrUtil.join(",", proUserList.stream().map(ProUserEntity::getProId).collect(Collectors.toList())));
        }
        return super.page(params);
    }

    @GetMapping("/getPage")
    @ApiOperation("分页列表-关联查询")
    public R<?> getPage(@RequestParam(name = "page", defaultValue = "1") Long page,
                        @RequestParam(name = "limit", defaultValue = "10") Long limit,
                        @RequestParam Map<String, Object> params) {
        return R.ok().data(proService.queryPage(new Page(page, limit), params));
    }

    @PostMapping("/savePro")
    @ApiOperation("添加项目")
    public R<ProEntity> savePro(@RequestBody ProEntity proEntity) {
        return proService.savePro(proEntity);
    }
}
